package com.cat.dataStructure03;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/construct-smallest-number-from-di-string/
 * @create 2025/11/16 14:47
 * @since JDK17
 */

class Solution70 {
    public String smallestNumber(String pattern) {
        int n = pattern.length();
        char cur = '1';
        char[] arr = new char[n + 1];
        for (int i = 0; i < n;) {
            if (i > 0 && pattern.charAt(i) == 'I') {
                i++;
            }
            for (; i < n && pattern.charAt(i) == 'I'; i++) {
                arr[i] = cur++;
            }
            int start = i;
            while (i < n && pattern.charAt(i) == 'D') {
                i++;
            }
            for (int j = i; j >= start; j--) {
                arr[j] = cur++;
            }
        }

        return new String(arr);
    }
}